<template>
  <page-header-wrapper :title="false">
    <a-card title="审批合同列表">
      <a-tabs type="card" @change="onTabs">
        <a-tab-pane v-for="(item,index) in tabOpts" :key="index" :tab="item"></a-tab-pane>
      </a-tabs>
      <div class="table-page-search-wrapper">
        <a-form-model :model="queryParam" layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="顾问姓名">
                <a-select v-model="queryParam['filter[adviser_uid]']" placeholder="请选择所属顾问">
                  <a-select-option v-for="(item,index) in adviserOpts" :key="index" :value="index">{{item}}</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="合同编号">
                <a-input v-model="queryParam['filter[contract_no]']" placeholder="请输入合同编号"/>
              </a-form-model-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-item label="审核时间">
                  <a-range-picker @change="onCheckTime" />
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="提交时间">
                  <a-range-picker @change="onSubmitCheckTime" />
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="包含开启">
                  <a-select v-model="queryParam['filter[fast]']" placeholder="请选择是否包含未开启业务">
                    <a-select-option value="1">是</a-select-option>
                    <a-select-option value="2">否</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="审核财务">
                  <a-select v-model="queryParam['filter[adviser_fid]']" placeholder="请选择审核财务">
                    <a-select-option v-for="(item,index) in financeOpts" :key="index" :value="index">{{item}}</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="有无尾款">
                  <a-select v-model="queryParam['filter[tail_money]']" placeholder="请选择有无尾款">
                    <a-select-option value="yes">有尾款</a-select-option>
                    <a-select-option value="no">无尾款</a-select-option>
                  </a-select>
                </a-form-model-item>
              </a-col>
            </template>
            <a-col :md="!advanced && 8 || 24" :sm="24">
              <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="reset">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <div class="table-operator">
        <s-table
          bordered
          ref="table"
          :row-key="(record) => record.contract_id"
          :columns="contractColumns"
          :data="contractData"
        >
          <template slot="adviser_name" slot-scope="text, record">
            <a-tag>{{text}}</a-tag><br>
            <a-tag>{{record.group_type}}组</a-tag><br>
            <a-tag>{{record.leader_name}}</a-tag>
          </template>
          <template slot="status" slot-scope="text, record">
            <a-button v-if="text == 34" size="small" icon="edit" type="link" @click="changeStatus(record.contract_id)">{{statusOpts[text]}}</a-button>
            <a-button v-if="text == 44" size="small" icon="edit" type="primary" @click="changeStatus(record.contract_id)">{{statusOpts[text]}}</a-button>
            <a-button v-if="text == 42" size="small" icon="info-circle" type="danger" @click="changeStatus(record.contract_id)">{{statusOpts[text]}}</a-button>
          </template>
          <template slot="action" slot-scope="text,record">
            <a-button size="small" type="primary" @click="$router.push({ name: 'ContractDetail', params: { id: record.contract_id } })">查看</a-button>
            <a-button size="small" type="danger" @click="abandonContract(record.contract_id)">废弃</a-button>
          </template>
        </s-table>
      </div>
    </a-card>
    <a-modal
      title="编辑合同状态"
      v-model="statusVisible"
      @ok="onSubmitStatus"
      :confirm-loading="loading"
      @cancel="onCancel"
    >
      <a-form-model
        ref="statusForm"
        :model="statusForm"
      >
        <a-form-model-item>
          <a-radio-group v-model="statusForm.status" @change="onChange">
            <a-radio v-for="(item,index) in statusOpt" :key="index" :value="index">{{item}}</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item>
          <a-textarea v-model="statusForm.notes" :rows="4"></a-textarea>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
    <a-modal
      centered
      title="废弃合同短信验证"
      v-model="smsVisible"
      :width="400"
      @ok="onCheckSms"
    >
      <a-form-model
        ref="ruleForm"
        layout="inline"
        :model="smsForm"
        :rules="smsRules"
      >
        <a-form-model-item prop="code">
          <a-input v-model="smsForm.code" type="text" placeholder="输入验证码" />
        </a-form-model-item>
        <a-form-model-item>
          <a-button
            type="primary"
            @click="sendSms"
            :disabled="btnSms"
            v-text="btnText"
          >
          </a-button>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </page-header-wrapper>
</template>

<script>
import { STable } from '@/components'
import financeColumns from '@/columns/finance'
import { abandonContract, checkSms, getContractList, getEditStatus, postEditStatus, sendSms } from '@/api/contract'

export default {
  components: {
    STable
  },
  name: 'CheckContract',
  data () {
    return {
      contractColumns: financeColumns.contract,
      contractData: parameters => {
        return getContractList(Object.assign(parameters, this.queryParam)).then(res => {
          this.tabOpts = res.result.tab_opts
          this.statusOpts = res.result.status_opts
          this.adviserOpts = res.result.all_advisers
          this.financeOpts = res.result.all_finances
          return res.result
        })
      },
      queryParam: {},
      advanced: true,
      tabOpts: {},
      statusOpts: {},
      adviserOpts: {},
      financeOpts: {},
      statusForm: {},
      statusOpt: {},
      statusVisible: false,
      smsForm: {},
      smsRules: {
        code: [{ required: true, message: '必须输入短信验证码', trigger: 'blur' }]
      },
      smsVisible: false,
      loading: false,
      btnSms: false,
      btnText: '发送短信',
      totalTime: 60,
      radio: '',
      abandonForm: {}
    }
  },
  created () {
    this.queryParam['filter[tab]'] = '0'
  },
  methods: {
    onTabs (key) {
      this.queryParam['filter[tab]'] = key
      this.$refs.table.refresh(true)
    },
    abandonContract (id) {
      this.abandonForm['id'] = id
      this.smsVisible = true
    },
    changeStatus (id) {
      this.statusForm = {}
      this.statusForm['contract_id'] = id
      getEditStatus(id).then(res => {
        if (res.result.status_opts === '') {
          this.$warning({
            title: '暂无权限编辑'
          })
        } else {
          this.statusOpt = res.result.status_opts
          this.statusVisible = true
        }
      })
    },
    onSubmitStatus () {
      const app = this
      const data = {}
      app.loading = true
      data['contract[status]'] = app.statusForm.status
      data['contract[notes]'] = app.statusForm.notes
      data['contract_id'] = app.statusForm.contract_id
      postEditStatus(data).then(res => {
        if (res.status === 0) {
          this.onCancel()
          app.$refs.table.refresh()
        } else {
          app.$warning({
            title: res.msg
          })
          app.loading = false
        }
      })
    },
    onCancel () {
      this.statusForm = {}
      this.statusVisible = false
      this.loading = false
    },
    onCheckTime (date, dateString) {
      this.queryParam['filter[check_time_start]'] = dateString[0]
      this.queryParam['filter[check_time_end]'] = dateString[1]
    },
    onSubmitCheckTime (date, dateString) {
      this.queryParam['filter[submit_check_time_start]'] = dateString[0]
      this.queryParam['filter[submit_check_time_end]'] = dateString[1]
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    sendSms () {
      const app = this
      sendSms().then(res => {
        if (res.status === 0) {
          app.btnSms = true
          const clock = setInterval(() => {
            app.totalTime--
            app.btnText = app.totalTime + 's后重新发送'
            if (app.totalTime < 0) {
              clearInterval(clock)
              app.btnText = '发送验证码'
              app.totalTime = 60
              app.btnSms = false
            }
          }, 1000)
        } else {
          this.$warning({
            title: res.msg
          })
        }
      })
    },
    onCheckSms () {
      const app = this
      app.$refs.ruleForm.validate(valid => {
        if (valid) {
          checkSms(app.smsForm).then(res => {
            if (res.status === 0) {
              abandonContract(app.abandonForm['id']).then(res => {
                app.smsVisible = false
                app.$refs.table.refresh()
              })
            } else {
              app.$warning({
                title: res.msg
              })
            }
          })
        } else {
          return false
        }
      })
    },
    onChange (e) {
      this.statusForm.status = e.target.value
    },
    reset () {
      const tab = this.queryParam['filter[tab]']
      this.queryParam = {}
      this.queryParam['filter[tab]'] = tab
      this.$refs.table.refresh(true)
    }
  }
}
</script>

<style scoped>

</style>
